package com.sxbbc.agent.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxbbc.agent.common.AccountMapping;
import com.sxbbc.agent.util.AgentSession;
import com.sxbbc.common.basics.rest.RestFactory;
import com.sxbbc.common.basics.rest.RestResponse;
import com.sxbbc.common.basics.utils.calculation.CalcuUtils;
import com.sxbbc.common.core.dto.jz.AccountDetailVO;
import com.sxbbc.common.core.dto.jz.TransVO;
import com.sxbbc.common.core.entity.jz.AgAgent;
import com.sxbbc.common.core.entity.jz.AgDonateDetail;
import com.sxbbc.common.core.entity.jz.AgMoneyDetail;
import com.sxbbc.common.core.entity.jz.AgShareDetail;
import com.sxbbc.common.core.enums.FundTypeEnums;
import com.sxbbc.common.core.enums.HandleTypeEnums;
import com.sxbbc.common.core.enums.TransTypeEnums;
import com.sxbbc.common.core.service.*;
import com.sxbbc.common.core.utils.BasePage;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@Controller
public class AccountController {

	@Autowired
	private HttpServletRequest request;
    @Autowired
    private IAgMoneyDetailService iAgMoneyDetailService;
    @Autowired
    private IAgShareDetailService iAgShareDetailService;
    @Autowired
    private IAgDonateDetailService iAgDonateDetailService;
    @Autowired
    private IAgAccountService iAgAccountService;
    @Autowired
    private IInfoParamService iInfoParamService;
    @Autowired
    private ITransService iTransService;
    
    
    
    /**
     * 
     * 账户明细页
     * @return
     * @author Wahyee
     */
    @RequestMapping(value = AccountMapping.SHOW_ACCOUNT_DETAIL)
    public ModelAndView showAccountDetail() {
        ModelAndView view = new ModelAndView(AccountMapping.SHOW_ACCOUNT_DETAIL);
        return view;
    }
    
    
    /**
     * 接口 金额明细
     *
     */
    @RequestMapping(value = AccountMapping.QUERY_ACCOUNT_DETAIL)
    @ResponseBody
    public RestResponse queryAccountDetail(AccountDetailVO vo,
    		BasePage<T> page) {
        AgAgent agent = AgentSession.getAgent(request);
        if (agent == null) {
            return RestFactory.fail("获取代理信息失败");
        }
        vo.setAgid(agent.getId());
        String timestr = vo.getTimestr();
        if(vo.getFundType() == null) {
        	vo.setFundType(FundTypeEnums.MONEY.getId());
        }
        if(StringUtils.isBlank(vo.getPayCode())) {
        	vo.setPayCode(null);
        }
        
        CalcuUtils calc = CalcuUtils.getInstance();
        if(calc.isEquals(vo.getFundType(), FundTypeEnums.MONEY.getId())) {
        	AgMoneyDetail entiy = new AgMoneyDetail();
        	BeanUtils.copyProperties(vo, entiy);
        	QueryWrapper<AgMoneyDetail> wrapper = new QueryWrapper<AgMoneyDetail>(entiy);
        	
        	wrapper.orderByDesc("create_time");
            if (!StringUtils.isBlank(timestr)) {
                String[] timeArr = timestr.split("~");
                if (!StringUtils.isBlank(timeArr[0]) && !StringUtils.isBlank(timeArr[1])) {
                    wrapper.between("create_time", timeArr[0], timeArr[1]);
                }
            }
            
        	Page<AgMoneyDetail> page1 = new BasePage<AgMoneyDetail>();
        	page1.setCurrent(page.getCurrent());
            iAgMoneyDetailService.page(page1, wrapper);
            for (AgMoneyDetail o : page1.getRecords()) {
                o.setTypeName(HandleTypeEnums.getName(o.getTypeId()));
            }
            return RestFactory.success(page1.getRecords(), page1.getTotal());
        }else if(calc.isEquals(vo.getFundType(), FundTypeEnums.DONATE.getId())){
        	AgDonateDetail entiy = new AgDonateDetail();
        	BeanUtils.copyProperties(vo, entiy);
        	QueryWrapper<AgDonateDetail> wrapper = new QueryWrapper<AgDonateDetail>(entiy);
        	
        	wrapper.orderByDesc("create_time");
            if (!StringUtils.isBlank(timestr)) {
                String[] timeArr = timestr.split("~");
                if (!StringUtils.isBlank(timeArr[0]) && !StringUtils.isBlank(timeArr[1])) {
                	wrapper.between("create_time", timeArr[0], timeArr[1]);
                }
            }
            
        	Page<AgDonateDetail> page1 = new BasePage<AgDonateDetail>();
        	page1.setCurrent(page.getCurrent());
            iAgDonateDetailService.page(page1, wrapper);
            for (AgDonateDetail o : page1.getRecords()) {
                o.setTypeName(HandleTypeEnums.getName(o.getTypeId()));
            }
            return RestFactory.success(page1.getRecords(), page1.getTotal());
        }else if(calc.isEquals(vo.getFundType(), FundTypeEnums.SHARE.getId())) {
        	AgShareDetail entiy = new AgShareDetail();
        	BeanUtils.copyProperties(vo, entiy);
        	QueryWrapper<AgShareDetail> wrapper = new QueryWrapper<AgShareDetail>(entiy);
        	
        	wrapper.orderByDesc("create_time");
            if (!StringUtils.isBlank(timestr)) {
                String[] timeArr = timestr.split("~");
                if (!StringUtils.isBlank(timeArr[0]) && !StringUtils.isBlank(timeArr[1])) {
                    wrapper.between("create_time", timeArr[0], timeArr[1]);
                }
            }
            
        	Page<AgShareDetail> page1 = new BasePage<AgShareDetail>();
        	page1.setCurrent(page.getCurrent());
        	iAgShareDetailService.page(page1, wrapper);
            for (AgShareDetail o : page1.getRecords()) {
                o.setTypeName(HandleTypeEnums.getName(o.getTypeId()));
            }
            return RestFactory.success(page1.getRecords(), page1.getTotal());
        }
        return RestFactory.success(page.getRecords(), page.getTotal());
    }
    
//    /**
//     *
//     * 页面 转账
//     * @return
//     * @author Wahyee
//     */
//    @RequestMapping(value = AccountMapping.SHOW_TRANS)
//    public ModelAndView showTrans() {
//    	ModelAndView view = new ModelAndView(AccountMapping.SHOW_TRANS);
//    	AgAgent agent = AgentSession.getAgent(request);
//    	if(agent == null) {
//    		throw new ThrowPageException("信息获取失败，请检查网络连接");
//    	}
//    	AgAccount account = iAgAccountService.selectByAgid(agent.getId());
//    	view.addObject("account", account);
//    	//转换比例
//    	BigDecimal scale = CalcuUtils.getInstance().getValueByClass(BigDecimal.class, iInfoParamService.getValueByCode(ParamEnums.SHARE_TO_MONEY));
//    	view.addObject("scale",scale);
//    	//手续费比例
//    	BigDecimal feeScale = CalcuUtils.getInstance().getValueByClass(BigDecimal.class, iInfoParamService.getValueByCode(ParamEnums.SHARE_TO_MONEY_FEE));
//    	view.addObject("feeScale",feeScale);
//    	return view;
//    }
    
    /**
     * 
     * 转账
     * @param vo
     * @return
     * @author Wahyee
     */
    @RequestMapping(value = AccountMapping.HANDLE_TRANS)
    @ResponseBody
    public RestResponse trans(TransVO vo) {
    	AgAgent agent = AgentSession.getAgent(request);
    	if(agent == null) {
    		return RestFactory.fail("代理信息获取失败，请检查网络");
    	}
    	vo.setType(TransTypeEnums.AG_SHARE_TO_MONEY.getId());
    	String r = iTransService.handleTrans(vo, agent.getId());
    	if(r.length() > 0) {
    		return RestFactory.fail(r);
    	}
    	return RestFactory.success("成功");
    }



}
